package cn.zime.iot.view;

import cn.zime.iot.entity.StudentDO;
import cn.zime.iot.handler.AddStudentViewHandler;

import javax.swing.*;
import java.awt.*;


public class AddStudentView extends JDialog {
    JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.CENTER,10,20));
    JLabel nameLabel = new JLabel("姓名:",JLabel.RIGHT);
    JTextField nameTxt = new JTextField();
    JLabel noLabel = new JLabel("学号:",JLabel.RIGHT);
    JTextField noTxt = new JTextField();
    JLabel homeLabel = new JLabel("家乡:",JLabel.RIGHT);
    JTextField homeTxt = new JTextField();
    JLabel mathLabel = new JLabel("数学成绩:",JLabel.RIGHT);
    JTextField mathTxt = new JTextField();
    JLabel enLabel = new JLabel("英语成绩:",JLabel.RIGHT);
    JTextField enTxt = new JTextField();
    JLabel javaLabel = new JLabel("Java成绩:",JLabel.RIGHT);
    JTextField javaTxt = new JTextField();
    JLabel mysqlLabel = new JLabel("MySQL成绩:",JLabel.RIGHT);
    JTextField mysqlTxt = new JTextField();
    JLabel ciscoLabel = new JLabel("Cisco成绩:",JLabel.RIGHT);
    JTextField ciscoTxt = new JTextField();
    JButton addBtn = new JButton("添加");

    AddStudentViewHandler addStudentViewHandler;
    public AddStudentView(MainView mainView){
        super(mainView,"添加学生",true);

        addStudentViewHandler = new AddStudentViewHandler(this,mainView);
        nameLabel.setPreferredSize(new Dimension(80,30));
        jPanel.add(nameLabel);
        nameTxt.setPreferredSize(new Dimension(200,30));
        jPanel.add(nameTxt);

        noLabel.setPreferredSize(new Dimension(80,30));
        jPanel.add(noLabel);
        noTxt.setPreferredSize(new Dimension(200,30));
        jPanel.add(noTxt);

        homeLabel.setPreferredSize(new Dimension(80,30));
        jPanel.add(homeLabel);
        homeTxt.setPreferredSize(new Dimension(200,30));
        jPanel.add(homeTxt);

        mathLabel.setPreferredSize(new Dimension(80,30));
        jPanel.add(mathLabel);
        mathTxt.setPreferredSize(new Dimension(200,30));
        jPanel.add(mathTxt);

        enLabel.setPreferredSize(new Dimension(80,30));
        jPanel.add(enLabel);
        enTxt.setPreferredSize(new Dimension(200,30));
        jPanel.add(enTxt);

        javaLabel.setPreferredSize(new Dimension(80,30));
        jPanel.add(javaLabel);
        javaTxt.setPreferredSize(new Dimension(200,30));
        jPanel.add(javaTxt);

        mysqlLabel.setPreferredSize(new Dimension(80,30));
        jPanel.add(mysqlLabel);
        mysqlTxt.setPreferredSize(new Dimension(200,30));
        jPanel.add(mysqlTxt);

        ciscoLabel.setPreferredSize(new Dimension(80,30));
        jPanel.add(ciscoLabel);
        ciscoTxt.setPreferredSize(new Dimension(200,30));
        jPanel.add(ciscoTxt);

        addBtn.addActionListener(addStudentViewHandler);
        jPanel.add(addBtn);

        Container contentPane = getContentPane();
        contentPane.add(jPanel);

        setSize(350,500);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);//销毁当前窗体
        setResizable(false);
        setVisible(true);
    }

    public StudentDO buildStudentDO() {
        StudentDO studentDO = new StudentDO();
        studentDO.setName(nameTxt.getText());
        studentDO.setNo(noTxt.getText());
        studentDO.setHomeTown(homeTxt.getText());
        studentDO.setMath(Double.valueOf(mathTxt.getText()));
        studentDO.setEnglish(Double.valueOf(enTxt.getText()));
        studentDO.setJava(Double.valueOf(javaTxt.getText()));
        studentDO.setMysql(Double.valueOf(mysqlTxt.getText()));
        studentDO.setCisco(Double.valueOf(ciscoTxt.getText()));
        return studentDO;
    }
}
